package com.example.quanlithuchi;

import java.util.ArrayList;
import java.util.List;

import Adapter.ThuchiAdapter;
import InterFaceIModel.ILoaiThuChi;
import InterFaceIModel.IThuchiModel;
import InterFaceIModel.LoaiThuChiModel;
import InterFaceIModel.ThuchiModel;
import Presenter.ThuChiPresenter;
import View.IThuchiView;
import ViewHoder.Thuchihoder;
import android.R.bool;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.RelativeLayout;

public class Thu_Chi_Activity extends Activity implements IThuchiView {

	Context context=this;
	Button btnAdd;
	Button btnthu,btnchi;
	AlertDialog dialog;
	ThuchiAdapter adapter;
	ListView listthuchi;
	ThuChiPresenter  presenter;
	RelativeLayout previousView;
	Button btnEdit_item,btnDelete_item;
	IThuchiModel thuchi_selected;
	long idThuchi;
	View view_tool;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_thu__chi_);
		setTitle("Quan li thu chi");
		btnAdd=(Button) findViewById(R.id.btnAddthuchi);
		btnAdd.setOnClickListener(onAdd);
		listthuchi=(ListView) findViewById(R.id.list_thuchi);
		listthuchi.setOnItemClickListener(onItemClickListener);
		presenter=new ThuChiPresenter(this);
		presenter.getAll();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_thu__chi_, menu);
		return true;
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		if (requestCode==1&&resultCode==1) {
			
			
			IThuchiModel model=new ThuchiModel();
			String khoanchi=data.getStringExtra("khoanchi");
			double sotien=data.getDoubleExtra("sotien", 0);
			long ngaychi=data.getLongExtra("ngaychi", 0);
			String ghichu=data.getStringExtra("ghichu");
			ILoaiThuChi loaiThuChi=new LoaiThuChiModel();
			String tenloaithuchi=data.getStringExtra("tenloaithuchi");
			long idloaithuchi=data.getLongExtra("idloaithuchi", 0);
			long idthuchi=data.getLongExtra("id", 0);
			int isthuchi=data.getIntExtra("isthuchi", 0);
			loaiThuChi.setId(idloaithuchi);
			loaiThuChi.setTenLoai(tenloaithuchi);
			loaiThuChi.setIsThucThi(isthuchi);
			model.setKhoanchi(khoanchi);
			model.setSotien(sotien);
			model.setLoaiThuchi(loaiThuChi);
			model.setGhichu(ghichu);
			model.setNgaychi(ngaychi);
			model.setId(idthuchi);
			adapter.add(model);
			adapter.notifyDataSetChanged();
		}
		if (requestCode==1&&resultCode==2) {
			
			IThuchiModel model=new ThuchiModel();
			String khoanchi=data.getStringExtra("khoanchi");
			double sotien=data.getDoubleExtra("sotien", 0);
			long ngaychi=data.getLongExtra("ngaychi", 0);
			String ghichu=data.getStringExtra("ghichu");
			ILoaiThuChi loaiThuChi=new LoaiThuChiModel();
			String tenloaithuchi=data.getStringExtra("tenloaithuchi");
			long idloaithuchi=data.getLongExtra("idloaithuchi", 0);
			long idthuchi=data.getLongExtra("id", 0);
			int isthuchi=data.getIntExtra("isthuchi", 0);
			loaiThuChi.setId(idloaithuchi);
			loaiThuChi.setTenLoai(tenloaithuchi);
			loaiThuChi.setIsThucThi(isthuchi);
			model.setKhoanchi(khoanchi);
			model.setSotien(sotien);
			model.setLoaiThuchi(loaiThuChi);
			model.setGhichu(ghichu);
			model.setNgaychi(ngaychi);
			model.setId(idthuchi);
			int index=adapter.getPosition(thuchi_selected);
			adapter.remove(thuchi_selected);
			adapter.insert(model, index);
			thuchi_selected=model;
			adapter.notifyDataSetChanged();
		}
	}

	OnClickListener onAdd=new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) 
			{
				case R.id.btnAddthuchi:
					
				{
					AlertDialog.Builder builder=new AlertDialog.Builder(context);
					builder.setTitle("Them giao dich");
					View view=getLayoutInflater().inflate(R.layout.giaodich, null);
					builder.setView(view);
					btnthu=(Button) view.findViewById(R.id.btnthu);
					btnchi=(Button) view.findViewById(R.id.btnchi);
					btnchi.setOnClickListener(onAdd);
				    btnthu.setOnClickListener(onAdd);
				     dialog=builder.create();
				    dialog.show();
				}
					break;
				case R.id.btnthu:
					
				{
					Intent intent=new Intent(context, ThuchidetailActivity.class);
					intent.putExtra("id", 0);
					intent.putExtra("isAdd", true);
					dialog.dismiss();
					startActivityForResult(intent, 1);
				}
					break;
				case R.id.btnchi:
					
				{
					Intent intent=new Intent(context, ThuchidetailActivity.class);
					intent.putExtra("id", 1);
					intent.putExtra("isAdd", true);
					dialog.dismiss();
					startActivityForResult(intent, 1);
					
				}
					break;

			
			}
		   
		}
	};
	@Override
	public void setListThuchi(List<IThuchiModel> list) {
		// TODO Auto-generated method stub
		if (list!=null) {
			adapter=new ThuchiAdapter(context, R.layout.thuchi_listitem, list);
			listthuchi.setAdapter(adapter);
		} else {
           list=new ArrayList<IThuchiModel>();
           adapter=new ThuchiAdapter(context, R.layout.thuchi_listitem, list);
			listthuchi.setAdapter(adapter);
		}
	}

	@Override
	public Context getContext() {
		// TODO Auto-generated method stub
		return context;
	}
	OnItemClickListener onItemClickListener=new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> arg0, View view, int arg2,
				long arg3) {
			// TODO Auto-generated method stub
			if (previousView!=null) {
				previousView.removeView(view_tool);
			}
			Thuchihoder thuchihoder=(Thuchihoder) view.getTag();
			thuchi_selected=(IThuchiModel) thuchihoder.txtLoaiThuchi.getTag();
			idThuchi=thuchi_selected.getId();
		RelativeLayout layout=	(RelativeLayout) view;
		previousView=layout;
		view_tool=getLayoutInflater().inflate(R.layout.tool_delete, null);
		int height=view.getHeight();
		view_tool.setPadding(10, height, 0, 0);
		btnEdit_item=(Button) view_tool.findViewById(R.id.btnEdit_item);
		
		btnDelete_item=(Button) view_tool.findViewById(R.id.btnDelete_item);
		btnEdit_item.setOnClickListener(onButtonClickListener);
		btnDelete_item.setOnClickListener(onButtonClickListener);
		layout.addView(view_tool);
			
			
		}
	};
	OnClickListener onButtonClickListener=new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.id.btnDelete_item:
			{
				int result=presenter.delete();
				if (result>0) {
					
					adapter.remove(thuchi_selected);
					adapter.notifyDataSetChanged();
					previousView.removeView(view_tool);
				}
			}
			break;
				
				case R.id.btnEdit_item:
				{
					Intent intent=new Intent(context,ThuchidetailActivity.class);
					intent.putExtra("khoanchi", thuchi_selected.getKhoanchi());
					intent.putExtra("sotien", thuchi_selected.getSotien());
					intent.putExtra("idloaithuchi", thuchi_selected.getLoaiThuChi().getId());
					intent.putExtra("tenloaithuchi", thuchi_selected.getLoaiThuChi().getTenLoai());
					intent.putExtra("isthuchi", thuchi_selected.getLoaiThuChi().getIsThuchi());
					intent.putExtra("ngaychi", thuchi_selected.getNgaychi());
					intent.putExtra("ghichu", thuchi_selected.getGhichu());
					intent.putExtra("id", thuchi_selected.getId());
					intent.putExtra("isAdd", false);
					startActivityForResult(intent, 1);
					
				}break;

			
			}
		}
	};
	@Override
	public long getId() {
		// TODO Auto-generated method stub
		return idThuchi;
	}
	

}
